package com.luoc.mybatis;

import com.luoc.mybatis.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisDemo {
    public static void main(String[] args) {
        // 读取配置文件
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获得会话
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 获取相关的执行器，获取用户mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 执行列表查询
//        List<User> userList = userMapper.list();
//        System.out.println(userList.toString());


        // 执行单条查询
//        User user = userMapper.selectById(1);
//        System.out.println(user.getName());

        // 执行插入
//        User user1 = new User();
//        user1.setName("罗绯红");
//        user1.setPassword("mima");
//        userMapper.insert(user1);
//        System.out.println(user1.getId());
//        // 执行更新提交
//        sqlSession.commit();
//        // 关闭会话
//        sqlSession.close();
        List<User> userCondition = userMapper.findUserCondition("张三", 0, "", "user", "");
        System.out.println(userCondition.toString());
    }
}
